from django import db
from django.http import JsonResponse
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse
from django.views import View

from housecentapp.models import Users
from housecentapp.utils.addpwd import loadpwd
from housecentapp.utils.mails import netmail
from housecentapp.utils.savehead import recevierpics


def nonefile(request):
    return redirect(reverse('house:index'))
def select(request):
    db.connection="select * from "

def index(request):
    if request.method=="GET":
        try:
            msg=request.session.get('msg')
            del request.session['msg']
        except:
            msg=""
        return render(request,'index.html',{"msg":msg})

def login(request):
    if request.method=="GET":
        return render(request,'login.html')
    elif request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
        print(username,password)
        return redirect(reverse('house:login'))

def register(request):
    if request.method=="GET":
        try:
            msg=request.session.get('msg')
            del request.session['msg']
        except:
            msg=""
        return render(request,'register.html',{"msg":msg})
    elif request.method == "POST":
        useraccount = request.POST.get('useraccount', None)
        if not useraccount:
            msg='登录名不能为空'
            request.session["msg"]=msg
            return redirect('/register.html')
        elif len(useraccount)<6:
            msg="登录名过短"
            request.session["msg"] = msg
            return redirect('/register.html')
        elif Users.objects.filter(useraccount=useraccount):
            msg = "登录名已被使用"
            request.session["msg"] = msg
            return redirect('/register.html')
        username = request.POST.get('username', None)
        if not username:
            msg='用户名不能为空'
            request.session["msg"] = msg
            return redirect('/register.html')
        password = request.POST.get('password', None)
        if not password:
            msg='密码不能为空'
            request.session["msg"] = msg
            return redirect('/register.html')
        elif len(useraccount)<6:
            msg="密码过短"
            request.session["msg"] = msg
            return redirect('/register.html')
        phone = request.POST.get('phone',None)
        email = request.POST.get('email', None)
        if not password:
            msg='邮箱不能为空'
            request.session["msg"] = msg
            return redirect('/register.html')
        seyzm = request.session.pop('yzm', '')

        yzm = request.POST.get('yzm', '')
        if not yzm:
            msg="请输入验证码"
            request.session["msg"] = msg
            return redirect('/register.html')
        elif seyzm !=yzm:
            msg = "验证码错误"
            request.session["msg"] = msg
            return redirect('/register.html')
        userimg = request.FILES.get('userimg')
        userimg=recevierpics(userimg)
        if not userimg:
            msg="请上传头像"
            request.session["msg"] = msg
            return redirect('/register.html')
        password=loadpwd(password)
        user = Users(useraccount=useraccount, username=username, userphone=phone,userpass=password, useremail=email, userimg=userimg)
        user.save()
        request.session["useraccount"] = useraccount
        msg = username+"注册成功"
        request.session["msg"] = msg
        return redirect("/login.html")

def email(request,emailadress):
    if request.method=="GET":
        yzm=netmail(emailadress)
        if yzm:
            request.session["yzm"]=yzm
            return JsonResponse({"msg":"ok"})
        else:
            return JsonResponse({"msg":"error"})

class Userapi(View):
    def post(self,request,*args,**kwargs):
        pass
